<template>
  <d2-container>
    <template slot="header">
    <div class="app-container calendar-list-container">
      <div class="filter-container">
        <el-input  size="small" @keyup.enter.native="handleFilter" style="width: 200px;margin-right: 10px;" class="filter-item" placeholder="姓名或账户" v-model="listQuery.name"> </el-input>
        <el-button size="small" class="filter-item" type="primary" icon="search" @click="handleFilter">搜索</el-button>
        <el-button size="small" class="filter-item"  v-permission:function="['stStudentInfoManager_btn_add']" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>
      </div>
    </div>
    </template>

    <el-table :key='tableKey' :data="list" v-loading="false" size="small" stripe highlight-current-row style="width: 100%">

      <el-table-column align="center" label="id" width="65">
      <template slot-scope="scope">
        <span>{{scope.row.id}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="">
      <template slot-scope="scope">
        <span>{{scope.row.stuNumber}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="">
      <template slot-scope="scope">
        <span>{{scope.row.password}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="入学日期">
      <template slot-scope="scope">
        <span>{{scope.row.hiredate}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="性别 0-男，1-女
">
      <template slot-scope="scope">
        <span>{{scope.row.gender}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="出生日期">
      <template slot-scope="scope">
        <span>{{scope.row.birth}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="学生姓名">
      <template slot-scope="scope">
        <span>{{scope.row.name}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="身份证号">
      <template slot-scope="scope">
        <span>{{scope.row.identityNumber}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="电话号码">
      <template slot-scope="scope">
        <span>{{scope.row.phone}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="照片">
      <template slot-scope="scope">
        <span>{{scope.row.photo}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="邮箱">
      <template slot-scope="scope">
        <span>{{scope.row.email}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="0-未删除 1-已删除">
      <template slot-scope="scope">
        <span>{{scope.row.isDeleted}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="创建时间">
      <template slot-scope="scope">
        <span>{{scope.row.gmtCreate}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="更新时间">
      <template slot-scope="scope">
        <span>{{scope.row.gmtModified}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="籍贯">
      <template slot-scope="scope">
        <span>{{scope.row.nativeplace}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="政治面貌 1-中共党员  2-共青团员 3-群众">
      <template slot-scope="scope">
        <span>{{scope.row.political}}</span>
      </template>
    </el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="150"> <template slot-scope="scope">
        <el-button v-permission:function="['stStudentInfoManager_btn_edit']" size="small" type="success" @click="handleUpdate(scope.row)">编辑
        </el-button>
        <el-button v-permission:function="['stStudentInfoManager_btn_del']" size="small" type="danger" @click="handleDelete(scope.row)">删除
        </el-button>
      </template> </el-table-column>
    </el-table>
    <template slot="footer">
      <div v-show="!listLoading" class="pagination-container">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
      </div>
    </template>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="" prop="stuNumber">
      <el-input v-model="form.stuNumber" placeholder="请输入"></el-input>
    </el-form-item>
        <el-form-item label="" prop="password">
      <el-input v-model="form.password" placeholder="请输入"></el-input>
    </el-form-item>
        <el-form-item label="入学日期" prop="hiredate">
      <el-input v-model="form.hiredate" placeholder="请输入入学日期"></el-input>
    </el-form-item>
        <el-form-item label="性别 0-男，1-女
" prop="gender">
      <el-input v-model="form.gender" placeholder="请输入性别 0-男，1-女
"></el-input>
    </el-form-item>
        <el-form-item label="出生日期" prop="birth">
      <el-input v-model="form.birth" placeholder="请输入出生日期"></el-input>
    </el-form-item>
        <el-form-item label="学生姓名" prop="name">
      <el-input v-model="form.name" placeholder="请输入学生姓名"></el-input>
    </el-form-item>
        <el-form-item label="身份证号" prop="identityNumber">
      <el-input v-model="form.identityNumber" placeholder="请输入身份证号"></el-input>
    </el-form-item>
        <el-form-item label="电话号码" prop="phone">
      <el-input v-model="form.phone" placeholder="请输入电话号码"></el-input>
    </el-form-item>
        <el-form-item label="照片" prop="photo">
      <el-input v-model="form.photo" placeholder="请输入照片"></el-input>
    </el-form-item>
        <el-form-item label="邮箱" prop="email">
      <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
    </el-form-item>
        <el-form-item label="0-未删除 1-已删除" prop="isDeleted">
      <el-input v-model="form.isDeleted" placeholder="请输入0-未删除 1-已删除"></el-input>
    </el-form-item>
        <el-form-item label="创建时间" prop="gmtCreate">
      <el-input v-model="form.gmtCreate" placeholder="请输入创建时间"></el-input>
    </el-form-item>
        <el-form-item label="更新时间" prop="gmtModified">
      <el-input v-model="form.gmtModified" placeholder="请输入更新时间"></el-input>
    </el-form-item>
        <el-form-item label="籍贯" prop="nativeplace">
      <el-input v-model="form.nativeplace" placeholder="请输入籍贯"></el-input>
    </el-form-item>
        <el-form-item label="政治面貌 1-中共党员  2-共青团员 3-群众" prop="political">
      <el-input v-model="form.political" placeholder="请输入政治面貌 1-中共党员  2-共青团员 3-群众"></el-input>
    </el-form-item>
        </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')"  size="small">取 消</el-button>
        <el-button v-if="dialogStatus=='create'"  size="small" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" size="small" @click="update('form')">确 定</el-button>
      </div>
    </el-dialog>

  </d2-container>
</template>

<script>
  import {
      page,
      addObj,
      getObj,
      delObj,
      putObj
  } from '@/api/chooseCourse/stStudentInfo/index';
  import { mapGetters } from 'vuex';
  export default {
    name: 'stStudentInfo',
    data() {
      return {
        form: {
        stuNumber : undefined,        password : undefined,        hiredate : undefined,        gender : undefined,        birth : undefined,        name : undefined,        identityNumber : undefined,        phone : undefined,        photo : undefined,        email : undefined,        isDeleted : undefined,        gmtCreate : undefined,        gmtModified : undefined,        nativeplace : undefined,        political : undefined          },
        rules: {
    stuNumber: [
  {
    required: true,
    message: '请输入',
    trigger: 'blur'
  }
],   password: [
  {
    required: true,
    message: '请输入',
    trigger: 'blur'
  }
],   hiredate: [
  {
    required: true,
    message: '请输入入学日期',
    trigger: 'blur'
  }
],   gender: [
  {
    required: true,
    message: '请输入性别 0-男，1-女',
    trigger: 'blur'
  }
],   birth: [
  {
    required: true,
    message: '请输入出生日期',
    trigger: 'blur'
  }
],   name: [
  {
    required: true,
    message: '请输入学生姓名',
    trigger: 'blur'
  }
],   identityNumber: [
  {
    required: true,
    message: '请输入身份证号',
    trigger: 'blur'
  }
],   phone: [
  {
    required: true,
    message: '请输入电话号码',
    trigger: 'blur'
  }
],   photo: [
  {
    required: true,
    message: '请输入照片',
    trigger: 'blur'
  }
],   email: [
  {
    required: true,
    message: '请输入邮箱',
    trigger: 'blur'
  }
],   isDeleted: [
  {
    required: true,
    message: '请输入0-未删除 1-已删除',
    trigger: 'blur'
  }
],   gmtCreate: [
  {
    required: true,
    message: '请输入创建时间',
    trigger: 'blur'
  }
],   gmtModified: [
  {
    required: true,
    message: '请输入更新时间',
    trigger: 'blur'
  }
],   nativeplace: [
  {
    required: true,
    message: '请输入籍贯',
    trigger: 'blur'
  }
],   political: [
  {
    required: true,
    message: '请输入政治面貌 1-中共党员  2-共青团员 3-群众',
    trigger: 'blur'
  }
]        },
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          name: undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0
      }
    },
    created() {
      this.getList();
    },
    computed: {
      ...mapGetters([
        'elements'
      ])
    },
    methods: {
      getList() {
        this.listLoading = true;
        page(this.listQuery).then(response => {
          this.list = response.rows;
          this.total = response.total;
          this.listLoading = false;
      })
      },
      handleFilter() {
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      handleCreate() {
        this.resetTemp();
        this.dialogStatus = 'create';
        this.dialogFormVisible = true;
      },
      handleUpdate(row) {
        getObj(row.id)
            .then(response => {
          this.form = response;
        this.dialogFormVisible = true;
        this.dialogStatus = 'update';
      });
      },
      handleDelete(row) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            })
            .then(() => {
          delObj(row.id)
      .then(() => {
          this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        });
        const index = this.list.indexOf(row);
        this.list.splice(index, 1);
      });
      });
      },
      create(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
        if (valid) {
        addObj(this.form)
        .then(() => {
        this.dialogFormVisible = false;
        this.getList();
        this.$notify({
        title: '成功',
        message: '创建成功',
        type: 'success',
        duration: 2000
        });
        })
        } else {
        return false;
        }
        });
      },
      cancel(formName) {
        this.dialogFormVisible = false;
        const set = this.$refs;
        set[formName].resetFields();
      },
      update(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
        if (valid) {
        this.dialogFormVisible = false;
        this.form.password = undefined;
        putObj(this.form.id, this.form).then(() => {
        this.dialogFormVisible = false;
        this.getList();
        this.$notify({
        title: '成功',
        message: '创建成功',
        type: 'success',
        duration: 2000
        });
        });
        } else {
        return false;
        }
        });
      },
      resetTemp() {
        this.form = {
          username: undefined,
          name: undefined,
          sex: '男',
          password: undefined,
          description: undefined
        };
      }
    }
  }
</script>
